﻿using DGZImp.MdsProject.Base;
using DGZImp.MdsProject.Devices.Base;
using DGZImp.MdsProject.Devices.Models;
using Dm.parser;
using Nomad.Iot;
using Nomad.Iot.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DGZImp.MdsProject.Devices.Helper
{
    public static class IotExtends
    {
        public static ByteTransformBase GetPlcByteTransform(this PlcServe plcServe)
        {
            if (plcServe is SiemensServe)
            {
                return (plcServe as SiemensServe).Plc.ByteTransform;
            }
            else if (plcServe is OmronServe)
            {
                return (plcServe as OmronServe).Plc.ByteTransform;
            }
            else if (plcServe is MelsecServe)
            {
                return (plcServe as MelsecServe).Plc.ByteTransform;
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        public static async Task<OperateResult> WriteDataAsync(this PlcServe plcServe, PlcWriteItem item)
        {
            var res = new OperateResult();
            if (plcServe is SiemensServe)
            {
                var plc = (plcServe as SiemensServe).Plc;
                switch (item.DataType)
                {
                    case DataTypeEnum.Bool:
                        res = await plc.WriteAsync(item.Address, Convert.ToBoolean(item.Value));
                        break;
                    case DataTypeEnum.Byte:
                        res = await plc.WriteAsync(item.Address, Convert.ToByte(item.Value));
                        break;
                    case DataTypeEnum.Int16:
                        res = await plc.WriteAsync(item.Address, Convert.ToInt16(item.Value));
                        break;
                    case DataTypeEnum.UInt16:
                        res = await plc.WriteAsync(item.Address, Convert.ToUInt16(item.Value));
                        break;
                    case DataTypeEnum.Int32:
                        res = await plc.WriteAsync(item.Address, Convert.ToInt32(item.Value));
                        break;
                    case DataTypeEnum.UInt32:
                        res = await plc.WriteAsync(item.Address, Convert.ToUInt32(item.Value));
                        break;
                    case DataTypeEnum.Int64:
                        res = await plc.WriteAsync(item.Address, Convert.ToInt64(item.Value));
                        break;
                    case DataTypeEnum.UInt64:
                        res = await plc.WriteAsync(item.Address, Convert.ToUInt64(item.Value));
                        break;
                    case DataTypeEnum.Float:
                    case DataTypeEnum.Double:
                        res = await plc.WriteAsync(item.Address, Convert.ToDouble(item.Value));
                        break;
                    case DataTypeEnum.String:
                        res = await plc.WriteAsync(item.Address, Convert.ToString(item.Value));
                        break;
                    default:
                        break;
                }
            }
            else if (plcServe is OmronServe)
            {
                var plc = (plcServe as OmronServe).Plc;
                switch (item.DataType)
                {
                    case DataTypeEnum.Bool:
                        res = await plc.WriteAsync(item.Address, Convert.ToBoolean(item.Value));
                        break;
                    case DataTypeEnum.Byte:
                        res = await plc.WriteAsync(item.Address, Convert.ToByte(item.Value));
                        break;
                    case DataTypeEnum.Int16:
                        res = await plc.WriteAsync(item.Address, Convert.ToInt16(item.Value));
                        break;
                    case DataTypeEnum.UInt16:
                        res = await plc.WriteAsync(item.Address, Convert.ToUInt16(item.Value));
                        break;
                    case DataTypeEnum.Int32:
                        res = await plc.WriteAsync(item.Address, Convert.ToInt32(item.Value));
                        break;
                    case DataTypeEnum.UInt32:
                        res = await plc.WriteAsync(item.Address, Convert.ToUInt32(item.Value));
                        break;
                    case DataTypeEnum.Int64:
                        res = await plc.WriteAsync(item.Address, Convert.ToInt64(item.Value));
                        break;
                    case DataTypeEnum.UInt64:
                        res = await plc.WriteAsync(item.Address, Convert.ToUInt64(item.Value));
                        break;
                    case DataTypeEnum.Float:
                    case DataTypeEnum.Double:
                        res = await plc.WriteAsync(item.Address, Convert.ToDouble(item.Value));
                        break;
                    case DataTypeEnum.String:
                        res = await plc.WriteAsync(item.Address, Convert.ToString(item.Value));
                        break;
                    default:
                        break;
                }
            }
            else if (plcServe is MelsecServe)
            {
                var plc = (plcServe as MelsecServe).Plc;
                switch (item.DataType)
                {
                    case DataTypeEnum.Bool:
                        res = await plc.WriteAsync(item.Address, Convert.ToBoolean(item.Value));
                        break;
                    case DataTypeEnum.Byte:
                        res = await plc.WriteAsync(item.Address, Convert.ToByte(item.Value));
                        break;
                    case DataTypeEnum.Int16:
                        res = await plc.WriteAsync(item.Address, Convert.ToInt16(item.Value));
                        break;
                    case DataTypeEnum.UInt16:
                        res = await plc.WriteAsync(item.Address, Convert.ToUInt16(item.Value));
                        break;
                    case DataTypeEnum.Int32:
                        res = await plc.WriteAsync(item.Address, Convert.ToInt32(item.Value));
                        break;
                    case DataTypeEnum.UInt32:
                        res = await plc.WriteAsync(item.Address, Convert.ToUInt32(item.Value));
                        break;
                    case DataTypeEnum.Int64:
                        res = await plc.WriteAsync(item.Address, Convert.ToInt64(item.Value));
                        break;
                    case DataTypeEnum.UInt64:
                        res = await plc.WriteAsync(item.Address, Convert.ToUInt64(item.Value));
                        break;
                    case DataTypeEnum.Float:
                    case DataTypeEnum.Double:
                        res = await plc.WriteAsync(item.Address, Convert.ToDouble(item.Value));
                        break;
                    case DataTypeEnum.String:
                        res = await plc.WriteAsync(item.Address, Convert.ToString(item.Value));
                        break;
                    default:
                        break;
                }
            }
            else
            {
                throw new NotImplementedException();
            }
            return res;
        }

    }
}
